#ifndef BATCHUPDATESTRATEGY_H
#define BATCHUPDATESTRATEGY_H

#include "istatusupdatestrategy.h"
#include <QTimer>
#include <QObject>

class StatusBarManager;

/**
 * BatchUpdateStrategy - 批量更新策略
 *
 * Martin Fowler重构：Strategy Pattern Implementation
 * 收集状态变化并批量应用，减少UI更新频率，提高性能
 *
 * 适用于：
 * - 高频状态变化场景
 * - 性能要求高的情况
 * - 大量数据处理时
 */
class BatchUpdateStrategy : public QObject, public IStatusUpdateStrategy
{
    Q_OBJECT

public:
    explicit BatchUpdateStrategy(StatusBarManager* statusBarManager = nullptr,
                               int batchIntervalMs = 100,
                               QObject* parent = nullptr);

    void setStatusBarManager(StatusBarManager* statusBarManager) {
        m_statusBarManager = statusBarManager;
    }

    void setBatchInterval(int intervalMs) {
        m_batchIntervalMs = intervalMs;
        m_batchTimer->setInterval(intervalMs);
    }

    // IStatusUpdateStrategy interface
    void applyUpdate(const StatusBarState& newState,
                    const StatusBarState& oldState,
                    void* context = nullptr) override;

    QString strategyName() const override {
        return QString("BatchUpdate(%1ms)").arg(m_batchIntervalMs);
    }

    bool supportsBatchUpdate() const override {
        return true;
    }

    void flushPendingUpdates() override;
    void reset() override;

private slots:
    void processBatchedUpdates();

private:
    StatusBarManager* m_statusBarManager;
    QTimer* m_batchTimer;
    int m_batchIntervalMs;

    // 挂起的状态更新
    bool m_hasPendingUpdates;
    StatusBarState m_pendingState;
    StatusBarState m_lastAppliedState;
};

#endif // BATCHUPDATESTRATEGY_H